<?php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use kartik\date\DatePicker;
use wbraganca\dynamicform\DynamicFormWidget;
use app\modules\Basic\models\Customer;
use kartik\select2\Select2;

//获取客户 
$customers = Customer::all();  
$customers = yii\helpers\ArrayHelper::map($customers,'id','name');

?>

<style type="text/css">
    .form-group{margin-bottom: 0;}
    .select2-container .select2-selection--single .select2-selection__rendered{margin-top: 0;}
</style>

<div class="agreement-form">

    <div class="header" style="text-align: center;margin-bottom: 20px;
    font-size: 16px;font-weight: bold;">
            销售合同信息
    </div>
    <?php $form = ActiveForm::begin([
                 'id' => 'dynamic-form',
                 'options' => ['enctype' => 'multipart/form-data',],
                 'layout' => 'horizontal',
                 //'enableAjaxValidation'=>true,
                 'fieldConfig' => [
                    'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
                    'horizontalCssClasses' => [
                        'label' => 'col-sm-3',
                        'wrapper' => 'col-sm-8',
                        'error' => '',
                    ],
                 ],
            ]); ?> 

    <div class="row">

        <div class="col-md-6">

            <?= $form->field($model, 'customer_id')->widget(Select2::classname(), [
                'data' => $customers,
                'language' => 'zh-cn',
                'options' => ['placeholder' => '选择客户'],
                'pluginOptions' => [
                    'allowClear' => true,
                ],
            ]); ?>

            <?= $form->field($model, 'order_type')->inline(true)->radioList(Yii::$app->params['order_type']) ?>

            <?= $form->field($model, 'sale_currency')->widget(Select2::classname(), [
                'data' => Yii::$app->params['currency_type'],
                'language' => 'zh-cn',
                'options' => ['placeholder' => '选择币种'],
                'pluginOptions' => [
                    'allowClear' => true,
                ],
            ]); ?>

            <?= $form->field($model, 'sale_price')->textInput(['maxlength' => true]) ?>

            <?= $form->field($model, 'type')->inline(true)->radioList(Yii::$app->params['contract_type']) ?>

            <?= $form->field($model, 'payment_type')->textInput(['maxlength' => true]) ?>

            <?= $form->field($model, 'payment_date')->textInput(['maxlength' => true]) ?>

            <?= $form->field($model, 'warranty')->textInput(['maxlength' => true]) ?>

            <?= $form->field($model, 'exemption')->textInput(['maxlength' => true]) ?>

            <?= $form->field($model, 'trade_company')->textInput(['maxlength' => true]) ?>

            <?= $form->field($model, 'trade_contract')->textInput(['maxlength' => true]) ?>

            <?= $form->field($model, 'invoice_no')->textInput(['maxlength' => true]) ?>

            <?= $form->field($model, 'trade_main')->textInput(['maxlength' => true]) ?>

            <?= $form->field($model, 'send_date')->widget(DatePicker::classname(),['options' => ['placeholder' => '发货日期'],'pluginOptions' => ['format' => 'yyyy-mm-dd','autoclose'=>true,'todayHighlight' => true]
            ]) ?>

            <?= $form->field($model, 'install_date')->widget(DatePicker::classname(),['options' => ['placeholder' => '安装日期'],'pluginOptions' => ['format' => 'yyyy-mm-dd','autoclose'=>true,'todayHighlight' => true]
            ]) ?>

            <?= $form->field($model, 'remarks')->textArea(['maxlength' => true]) ?>
        </div>

        <div class="col-md-6">

            <?php DynamicFormWidget::begin([
                'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric 
                'widgetBody' => '.container-items', // required: css class selector
                'widgetItem' => '.item', // required: css class
                'limit' => 999, // the maximum times, an element can be cloned (default 999)
                'min' => 1, // 0 or 1 (default 1)
                'insertButton' => '.add-item', // css class
                'deleteButton' => '.remove-item', // css class
                'model' => $goods[0],
                'formId' => 'dynamic-form',
                'formFields' => [
                    'agreement_id',
                    'name',
                ],
            ]); ?>

        <div class="col-md-2">
            货品名称
        </div>
        <div class="col-md-10">
            <div class="container-items">
            <?php foreach ($goods as $index => $good): ?>
                    <div class="col-md-12 item">
                        <?php
                            if (!$good->isNewRecord) {
                                echo Html::activeHiddenInput($good, "[{$index}]id");
                            }
                        ?>
                        <div class="col-md-10">
                        <?= $form->field($good, "[{$index}]name")->label(false)->textInput(['maxlength' => true,'placeholder'=> '货品名称']);?>
                        </div> 
                        <div class="col-md-2">
                            <button type="button" class="remove-item btn btn-danger btn-xs"><i class="fa fa-minus"></i></button>
                        </div>    
                    </div>
            <?php endforeach; ?>
            </div>   
        </div>

        <div class="pull-right">
            <button class="btn btn-primary add-item">添加货品</button>
        </div>       
        <?php DynamicFormWidget::end(); ?>
    
        </div>
    </div>

    <div class="form-group text-center">
        <?= Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
